package com.cp.controller;

import com.cp.common.BaseController;
import com.cp.repository.*;
import com.cp.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Controller
public class IndexController extends BaseController {
	Logger log = LogManager.getLogger();

	@Autowired
	ConvictionService convictionService;

	@Autowired
	ConvictionClassService convictionClassService;

	@Autowired
	CodeSemanticChunkService codeSemanticChunkService;

	@Autowired
	ClassRelationService classRelationService;

	@Autowired
	AdjudgmentRelationWordService adjudgmentRelationWordService;

	@Autowired
	AdjudgmentWordService adjudgmentWordService;

	@Autowired
	WordFrqMiService wordFrqMiService;

	@Autowired
	LuceneService luceneService;

	@Autowired
	FormatJSON fs;



	@RequestMapping("/index")
	public String index(@RequestParam(name = "convictionId", defaultValue = "0") Integer convictionId, @RequestParam(name = "codeSemanticChunkId", defaultValue = "0") Integer codeSemanticChunkId, @RequestParam(name = "keyWord", defaultValue = "") String keyWord, Model model) {
		System.out.println(convictionId);
		System.out.println(codeSemanticChunkId);
		String relationIds1 = "7,2,8,11,1";
		String relationIds2 = "5, 3 ,17, 9, 22, 6, 10, 4";
		String sampleJson = "";
		String adjudgmentWordJson = "";
		String frameNetJson = "";
		String subFrameNetJson = "";

		if(convictionId==0||codeSemanticChunkId==0) {

		} else {


			//pageA
			try {
				CodeSemanticChunk csc = codeSemanticChunkService.getById(codeSemanticChunkId);
				frameNetJson = fs.formatCodeSemanticChunk(csc, codeSemanticChunkService.getByConvictionId(csc.getConviction().getId()), classRelationService.getAll());

				//pageB
				subFrameNetJson = fs.yycc(csc);
			} catch (Exception ex) {
				ex.printStackTrace();
				log.error(ex.getMessage());
			}

			//pageC
			try {
				adjudgmentWordJson = fs.formatAdjudgmentWordJson(adjudgmentWordService.getByRelationWordByConvictionIdAndClassRelationIds(convictionId, relationIds2), "同近义词");
				System.out.println("adjudgmentWordJson:"+adjudgmentWordJson);
			} catch (Exception ex) {
				ex.printStackTrace();
				log.error(ex.getMessage());
			}

			//pageD
			try {
				sampleJson = fs.formatAdjudgmentWordJson2(adjudgmentWordService.getByRelationWordByConvictionIdAndClassRelationIds(convictionId, relationIds1), "相关实例");
				//System.out.println("sampleJson" + sampleJson);

			} catch (Exception ex) {
				ex.printStackTrace();
				log.error(ex.getMessage());
			}

		}


		model.addAttribute("convictionId", convictionId);
		model.addAttribute("codeSemanticChunkId", codeSemanticChunkId);
		model.addAttribute("keyWord", keyWord);
		if(keyWord.indexOf("--")>1){
			model.addAttribute("keyWord2", "（ "+keyWord.replace("--", "）  "));
		} else {
			model.addAttribute("keyWord2", keyWord);
		}

		model.addAttribute("subFrameNetJson", subFrameNetJson);
		model.addAttribute("frameNetJson", frameNetJson);
		model.addAttribute("adjudgmentWordJson", adjudgmentWordJson);
		model.addAttribute("sampleJson", sampleJson);
		return "index.html";
	}

	@RequestMapping("/index2")
	public String index2(Model model) {
		model.addAttribute("convictionAll", convictionService.getAll());
		model.addAttribute("classRelationAll", classRelationService.getAll());
		return "index2.html";
	}

	/**
	 * 语义场框架
	 * @param id
	 * @return
	 */
	@RequestMapping("/yyckj/{id}")
	@ResponseBody
	public String yyckj(@PathVariable(name = "id") Integer id) {
		CodeSemanticChunk csc = codeSemanticChunkService.getById(id);
		return fs.formatCodeSemanticChunk(csc, codeSemanticChunkService.getByConvictionId(csc.getConviction().getId()), classRelationService.getAll());
	}

	/**
	 * 动态提示列表
	 * @param keyWord
	 * @return
	 */
	@RequestMapping("/getInputSeggest/{keyWord}")
	@ResponseBody
	public String getInputSeggest(@PathVariable(name = "keyWord") String keyWord) {
		//Integer classRelationId = new Integer(3);
		return fs.formatInputSuggest(codeSemanticChunkService.getBySemanticChunkAndClassRelationId(keyWord, null), keyWord);
	}

	/**
	 * 相关语义场景框架
	 * @param id
	 * @return
	 */
	@RequestMapping("/xgyycjkj/{id}")
	@ResponseBody
	public List<CodeSemanticChunk> xgyycjkj(@PathVariable(name = "id") Integer id) {
		CodeSemanticChunk csc = codeSemanticChunkService.getById(id);
		System.out.println("相关语义场景框架");
		return codeSemanticChunkService.getBySemanticChunk(csc.getContent());
	}

	/**
	 * 获取某罪名下相关同义词候选列表
	 * @return
	 */
	@RequestMapping("/getAdjudmentRelationWord")
	@ResponseBody
	public List<AdjudgmentRelationWord> getAdjudmentRelationWord(@RequestParam(name = "convictionId") Integer convictionId){
		return adjudgmentRelationWordService.getByConvictionId(convictionId);
	}

	/**
	 * 获取候选列表中同义词的相关词
	 * @param convictionId
	 * @return
	 */
	@RequestMapping("/getAdjudgmentWordByConvictionId/{convictionId}")
	@ResponseBody
	public String getAdjudgmentWordByConvictionId(@PathVariable(name = "convictionId") Integer convictionId){
		//return fs.formatAdjudgmentWordJson(adjudgmentWordService.getByRelationId(relationId));
		String relationIds = "5, 3 ,17, 9, 22, 6, 10, 4";
		List<AdjudgmentWord> list =adjudgmentWordService.getByRelationWordByConvictionIdAndClassRelationIds(convictionId, relationIds);
		String json = fs.formatAdjudgmentWordJson(list, "同近义例词");
		System.out.println("同义词：" + json);
		return json;
	}


	/**
	 * 判例中的实例
	 * @param convictionId
	 * @return
	 */
	@RequestMapping("/getSample/{convictionId}")
	@ResponseBody
	public String getSample(@PathVariable(name = "convictionId") Integer convictionId) {
		//return fs.formatSampleJson(convictionId);
		String relationIds = "7,2,8,11,1";
		String json = fs.formatAdjudgmentWordJson2(adjudgmentWordService.getByRelationWordByConvictionIdAndClassRelationIds(convictionId, relationIds), "相关实例");
		System.out.println("实例: " + json);
		return json;
	}

	/**
	 * 根据罪名ID获取词频与互信息中不重复的框架ID
	 * @param convictionId 罪名ID
	 * @return
	 */
	@RequestMapping("/getWordFrqMiClassRelationIdByConvictionId")
	@ResponseBody
	public List<WordFrqMi> getWordFrqMiClassRelationIdByConvictionId(@RequestParam(name = "convictionId") Integer convictionId) {
		return wordFrqMiService.getByConvictionId(convictionId);
	}

	/**
	 * 获取词频与互信息
	 * @param convictionId 罪名ID
	 * @param classRelationId 框架ID
	 * @return
	 */
	@RequestMapping("/getWordFrqMiByconvictionIdAndClassRelationId")
	@ResponseBody
	public List<WordFrqMi> getWordFrqMiByConvictionIdAndClassRelationId(@RequestParam(name = "convictionId") Integer convictionId, @RequestParam(name = "classRelationId") Integer classRelationId){
		return wordFrqMiService.getByConvictionIdAndClassRelationId(convictionId, classRelationId);
	}

	/**
	 *
	 * @param convictionId
	 * @param classRelationId
	 * @return
	 */
	@RequestMapping("/getByConvictionIdOrClassRelationId")
	@ResponseBody
	public PageInfo<CodeSemanticChunk> getByConvictionIdOrClassRelationId(@RequestParam(name = "convictionId") Integer convictionId, @RequestParam(name = "classRelationId") Integer classRelationId, @RequestParam(name = "pageNumber") Integer pageNumber) {
		pageNumber = pageNumber == null ? 1 : pageNumber;
		PageHelper.startPage(pageNumber, 20);
		PageInfo<CodeSemanticChunk> pageInfo = new PageInfo<>(codeSemanticChunkService.getByConvictionIdOrClassRelationId(convictionId, classRelationId, pageNumber));
		return pageInfo;
	}

	@RequestMapping("/luceneSearch")
	@ResponseBody
	public Map<String, Object> luceneSearch(@RequestParam(name = "keyWord") String keyWord, @RequestParam(name = "pageNumber") Integer pageNumber) {
		pageNumber = pageNumber == null ? 1 : pageNumber;
		if(keyWord ==null || "".equals(keyWord)) {
			return null;
		} else {
			if(keyWord.contains("--")){
				keyWord = keyWord.substring(keyWord.lastIndexOf("--")).replaceAll("[\\-]", "").trim();
				if(keyWord.length()>0){
					return luceneService.search(keyWord, pageNumber);
				}
			} else {
				keyWord = keyWord.trim();
				return luceneService.search(keyWord, pageNumber);
			}
		}
		return null;
	}
}
